/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package iamutkarshtiwari.github.io.ananas.editimage.utils;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

/**
 * 加载进度view
 */
public class LoadingView {

    private Context mContext;
    private Component mComponent;
    private CommonDialog mCommonDialog;
    private AnimatorProperty animatorProperty;

    private LoadingView(Context context, Component component) {
        this.mContext = context;
        this.mComponent = component;

        mCommonDialog = new CommonDialog(mContext);
    }


    /**
     * 初始化
     *
     * @param context   Context
     * @param component Component
     * @return LoadingView
     */
    public static LoadingView decorate(Context context, Component component) {
        return new LoadingView(context, component);
    }

    /**
     * 重载 初始化
     *
     * @param context     Context
     * @param componentId 布局
     * @return LoadingView
     */
    public static LoadingView decorate(Context context, int componentId) {
        Component component = LayoutScatter.getInstance(context).parse(componentId, null, true);
        return new LoadingView(context, component);
    }

    /**
     * 设置加载view 大小
     *
     * @param width  宽
     * @param height 高
     * @return LoadingView
     */
    public LoadingView setSize(int width, int height) {
        mCommonDialog.setSize(900, 230);
        return this;
    }

    /**
     * 构建
     *
     * @return LoadingView
     */
    public LoadingView build() {
        mCommonDialog.setContentCustomComponent(mComponent);
        return this;
    }

    /**
     * 显示loading
     *
     * @param component Component
     * @return LoadingView
     */
    public LoadingView show(Component component) {
        mCommonDialog.show();

        if (component != null) {
            setAnimator(component);
        }

        return this;
    }

    /**
     * 隐藏loading
     *
     * @return LoadingView
     */
    public LoadingView hide() {
        mCommonDialog.hide();
        return this;
    }

    /**
     * 给view设置指定动画
     *
     * @param component Component
     */
    public void setAnimator(Component component) {
        animatorProperty = component.createAnimatorProperty();
        animatorProperty.rotate(360).setDuration(1000).setDelay(100).setLoopedCount(Animator.INFINITE);
        animatorProperty.start();
    }
}
